package com.tongda.menu.activity;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.json.JSONException;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

import com.tongda.core.common.Constants;
import com.tongda.core.service.BaseService;
import com.tongda.core.utils.CommunicationUtil;
import com.tongda.core.utils.SharedPreferencesUtils;
import com.tongda.core.vo.Employee;
import com.tongda.menu.R;

public class BaseActivity extends Activity{
	private static final String LOG_TAG = "BASEACTIVITY";
	public static final String SUPER_USER_NAME ="super";
	public static final String SUPER_USER_PASSWORD ="mixi8canyin";
	
	protected Thread loadDataThread;
	private boolean isBlockScreen;
	
	@SuppressLint("HandlerLeak")
	protected Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			try {
				switch (msg.what) {
				case Constants.DESCRIBE:
					describeView();
					break;
				case Constants.RE_DESCRIBE:
					reDescribeView();
					break;
				}
			} catch (Exception e) {
				Log.e(LOG_TAG, "HandleMessage Exception");
				e.printStackTrace();
			}
		}
	};
	//决定SplashActivity中是否更新菜谱的标志
	private static boolean isUpdateDishes;
	
	private String menuUserName;
	private String menuPassWord;
	
	protected void initCurrentView() {
		try {
			loadDataThread = new Thread(new Runnable() {
				@Override
				public void run() {
					loadData();
					handler.sendEmptyMessage(Constants.DESCRIBE);
				}
			});
			loadDataThread.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	protected void reInitCurrentView() {
		try {
			loadDataThread = new Thread(new Runnable() {
				@Override
				public void run() {
					reLoadData();
					handler.sendEmptyMessage(Constants.RE_DESCRIBE);
				}
			});
			loadDataThread.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	protected void describeView() {
		
	}
	
	protected void reDescribeView() {
		
	}
	
	protected void loadData() {
		
	}
	protected void reLoadData() {
		
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		menuUserName = (String) SharedPreferencesUtils.getParam(this, Constants.USERNAME, SUPER_USER_NAME);
		menuPassWord = (String) SharedPreferencesUtils.getParam(this, Constants.PASSWORD, SUPER_USER_PASSWORD);
		if("".equals(menuUserName))menuUserName=SUPER_USER_NAME;
		if("".equals(menuPassWord))menuPassWord=SUPER_USER_PASSWORD;
		try {
			BaseService.COOKIE_STR = Constants.COOKIE_STR_USERNAME + URLEncoder.encode(menuUserName, "utf-8") + Constants.COOKIE_STR_PASSWORD + menuPassWord;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		CommunicationUtil.SESSION_ID = (String) SharedPreferencesUtils.getParam(this, Constants.SESSIONID, CommunicationUtil.SESSION_ID);
		Constants.SERVER_IP = (String) SharedPreferencesUtils.getParam(this, "server_ip", Constants.SERVER_IP);
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main_menu, menu);
		return true;
	}


	private String employee2Json(Employee currentUser){
		JSONObject object = new JSONObject();
		try {
			object.put("empId", currentUser.getEmpId());
			object.put("address", currentUser.getAddress());
			object.put("adrCity", currentUser.getAdrCity());
			object.put("adrProvince", currentUser.getAdrProvince());
			object.put("emerConTel", currentUser.getEmerConTel());
			object.put("emerContact", currentUser.getEmerContact());
			object.put("empNum", currentUser.getEmpNum());
			object.put("gender", currentUser.getGender());
			object.put("idCard",currentUser.getIdCard() );
			object.put("jobPic", currentUser.getJobPic());
			object.put("loginUsername", currentUser.getLoginUsername());
			object.put("loginPassword", currentUser.getLoginPassword());
			object.put("mobile", currentUser.getMobile());
			object.put("name", currentUser.getName());
			object.put("restaurantId",currentUser.getRestaurantId());
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return object.toString();
	}
	
	private Employee json2Employee(String json){
		Employee employee = new Employee();
		try {
			JSONObject object = new JSONObject(json);
			employee.setEmpId(object.getString("empId"));
			employee.setAddress(object.getString("address"));
			employee.setAdrCity(object.getString("adrCity"));
			employee.setAdrProvince(object.getString("adrProvince"));
			employee.setEmerConTel(object.getString("emerConTel"));
			employee.setEmerContact(object.getString("emerContact"));
			employee.setEmpNum(object.getString("empNum"));
			employee.setGender(object.getString("gender"));
			employee.setIdCard(object.getString("idCard"));
			employee.setJobPic(object.getString("jobPic"));
			employee.setLoginUsername(object.getString("loginUsername"));
			employee.setLoginPassword(object.getString("loginPassword"));
			employee.setMobile(object.getString("mobile"));
			employee.setName(object.getString("name"));
			employee.setRestaurantId(object.getString("restaurantId"));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return employee;
	}

	public static boolean isUpdateDishes() {
		return isUpdateDishes;
	}

	public static void setUpdateDishes(boolean isUpdateDishes) {
		BaseActivity.isUpdateDishes = isUpdateDishes;
	}
	public boolean isBlockScreen() {
		return isBlockScreen;
	}

	public void setBlockScreen(boolean isBlockScreen) {
		this.isBlockScreen = isBlockScreen;
	}
}
